<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="编码"></el-input>
    </el-form-item>
    <el-form-item label="在途状态名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="在途状态名称"></el-input>
    </el-form-item>
    <el-form-item label="标识" prop="flag">
      <el-input v-model="dataForm.flag" placeholder="标识"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="ischeck">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getEimsInTransitObj, addEimsInTransitObj, putEimsInTransitObj} from '@/api/eims/intransit'

    export default {
    data () {
      return {
        visible: false,
        ischeck: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          flag: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: ''
        },
        dataRule: {
          code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '在途状态名称不能为空', trigger: 'blur' }
          ],
          flag: [
            { required: true, message: '标识不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getEimsInTransitObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
              	this.dataForm = response.data.data
              }else{
              	this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putEimsInTransitObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                  	this.$message.success('修改成功')
                  	this.visible = false
                  	this.$emit('refreshDataList')
                  }else{
                  	this.$message.error(response.data.msg)
                  }
                  this.ischeck = false
                }).catch(() => {
                  this.ischeck = false
                })
            } else {
                addEimsInTransitObj(this.dataForm).then(response => {
                  if(response.data.code == '0'){
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                  }else{
                    this.$message.error(response.data.msg)
                  }
                  this.ischeck = false
                }).catch(() => {
                  this.ischeck = false
                })
            }
          }else{
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
